// run: $exec < input
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>

struct data
{
	std::string name;
	int count;
};
bool operator<(data const& a, data const& b)
{
	return a.count > b.count || (a.count == b.count && a.name < b.name);
}

std::vector<data> da;
int n;

int main()
{
	std::ios::sync_with_stdio(false);
	while (std::cin >> n && n) {
		da.resize(n);
		for (int i = 0; i < n; i++)
			std::cin >> da[i].name >> da[i].count;

		std::sort(da.begin(), da.end());
		for (auto i : da)
			std::cout << i.name << " " << i.count << "\n";
		std::map<int, int> mm;
		std::map<std::string, int> all;
		for (int i = 0; i < n; i++) {
			mm[da[i].count]++;
			all[da[i].name] = i;
		}
		int q; std::cin >> q;
		for (int i = 0; i < q; i++) {
			std::string s; std::cin >> s;
			int now = da[all[s]].count;
			int t = 0;
			for (; da[t].count > now; t++);
			std::cout << t + 1;
			if (all[s] - t + 1 > 1) std::cout << " " << all[s] - t + 1 << "\n";
			else std::cout << "\n";
		}
	}
}

